
#include <jrtplib3/rtpsession.h>
#include <jrtplib3/rtpsessionparams.h>
#include <jrtplib3/rtpudpv4transmitter.h>
#include <jrtplib3/rtppacket.h>

#include "stdio.h"
#include "iostream"

using namespace jrtplib;
using namespace std;

void checkerror(int rtperr)
{
	if (rtperr < 0)
	{
		std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
		exit(-1);
	}
}

int main(void)
{
	RTPSession sess;
	uint16_t portbase = 1554;

	cout << "Using JRTPLIB version;" << RTPLibraryVersion::GetVersion().GetVersionString() << endl;
	cout << "Enter local portbase:" << portbase << endl;

	RTPUDPv4TransmissionParams transparams;
	transparams.SetPortbase(portbase);
	RTPSessionParams sessparams;
	sessparams.SetOwnTimestampUnit(1.0 / 10.0);
	sessparams.SetAcceptOwnPackets(true);
	int status = sess.Create(sessparams, &transparams);
	checkerror(status);

	while (1)
	{
		sess.BeginDataAccess();
		if (sess.GotoFirstSourceWithData())
		{
			do
			{
				RTPPacket *pack;
				while ((pack = sess.GetNextPacket()) != NULL)
				{
					cout << "Got packet !" << endl;
					cout << pack->GetPayloadData() << endl;
					sess.DeletePacket(pack);
				}
			} while (sess.GotoNextSourceWithData());
		}
		sess.EndDataAccess();
		RTPTime::Wait(RTPTime(1, 0));
	}
	sess.BYEDestroy(RTPTime(10, 0), 0, 0);

	printf("Hello World\n");
	return 0;
}